package com.zhijiaoyun.controller.api.mobile;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.controller.base.BaseController;

/**
 * @author shisfish
 * @date 2017/12/18
 */

@Controller
@RequestMapping(value = "/api/mobile/user")
public class UserMobileApi extends BaseController {

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Map<String, Object> login(String email, String password, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmptyString(email)) {
            return responseLoginError();
        }
        try {
            email = URLDecoder.decode(email, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return responseLoginError();
        }
        if (StringUtils.isEmptyString(password)) {
            return responseLoginError();
        }
        return loginService.userLogin(email, password, "mobile", request, response);
    }
	
    // 我的学习历史一级
    @ResponseBody
    @RequestMapping(value = "/courseHistoryList", method = RequestMethod.GET)
    public Map<String, Object> courseHistoryList1(int pageIndex, int pageSize, Long categorySuper, String name, HttpServletRequest request) {
        long userId = StringUtils.parseLong(String.valueOf(request.getAttribute("userId")), 0);
        if (pageIndex < 0) {
            pageIndex = 1;
        }
        if (pageSize < 0) {
            pageSize = DsConstant.PAGESIZE_FOUR;
        }
        return courseService.pageCourseHistoryByParams(pageIndex, pageSize, userId, categorySuper, name);
    }

}
